package com.free.food.order.mq.receiver;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 类功能描述：接收mq消息
 *
 * @author：刘富国
 * @createTime：2019-02-27 16:29
 */
@Component
public class TestReceiver {

    /**
     * 1.接收已有队列
     * 需要事先在rabbitmq上手动创建好"testQueues"队列
     */
    @RabbitListener(queues = "testQueues")
    public void process(String message) {
        System.out.println("testQueues收到消息:" + message);
    }

    /**
     * 2.自动创建队列
     */
    @RabbitListener(queuesToDeclare = @Queue("autoCreateQueues"))
    public void process2(String message) {
        System.out.println("autoCreateQueues收到消息:" + message);

    }

    /**
     * 3.自动创建队列，Exchange和Queue绑定
     * Exchange可以看成消息的分组,
     * 如果有两个消息队列绑定了同一个exchange，那么向exchange发消息的时候，这两个消息队列都能收到消息
     * 如果一个消息队列绑定了两个exchange，那么往任意一个exchange发消息的时候，这个消息队列都能收到消息
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue("bindQueues"), exchange = @Exchange("oneExChange"), key = "one"))
    public void process3(String message) {
        System.out.println("bindQueues，oneExChange，one收到消息:" + message);

    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("bindQueues"), exchange = @Exchange("twoExChange"), key = "two"))
    public void process4(String message) {
        System.out.println("bindQueues，twoExChange，two收到消息:" + message);
    }

}
